package cn.janescott.inner.space.lib.support.adapter;

import cn.janescott.inner.space.lib.support.pagination.PaginationResultDTO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * bootstrap table 适配器
 * @author scott
 * @date 2018/6/4
 */
public class BootstrapTableDTO<T> implements java.io.Serializable {

    private Integer total;

    private List<T> rows;

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public static <T> BootstrapTableDTO<T> assemble(PaginationResultDTO<T> source) {
        BootstrapTableDTO<T> result = new BootstrapTableDTO<>();
        if (null == source || CollectionUtils.isEmpty(source.getResult())) {
            result.setTotal(0);
            result.setRows(new ArrayList<>());
        } else {
            result.setRows(source.getResult());
            result.setTotal(source.getTotal());
        }
        return result;
    }

    public static <T> BootstrapTableDTO<T> assemble(List<T> source) {
        BootstrapTableDTO<T> result = new BootstrapTableDTO<>();
        if (CollectionUtils.isEmpty(source)) {
            result.setTotal(0);
            result.setRows(new ArrayList<>());
        } else {
            result.setTotal(source.size());
            result.setRows(source);
        }

        return result;
    }
}
